import Vue from 'vue';
String.prototype.gblen = function () {
    var len = 0;
    for (var i = 0; i < this.length; i++) {
        if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) {
            len += 2;
        } else {
            len++;
        }
    }
    return len;
};
String.prototype.subStr = function (len) {
    var nStr = '';
    if (this.gblen() < len) {
        return nStr;
    }
    var i = 1;
    while (nStr.gblen() < len - 0.5) {
        nStr = this.substr(0, i++);
    }
    return nStr;
};
let utils = {
    width: document.documentElement.clientWidth,
    height: document.documentElement.clientHeight,
    //是否存在指定函数
    isFunction: function (funcName) {
        try {
            if (typeof eval(funcName) == 'function') {
                return true;
            }
        } catch (e) {}
        return false;
    },
    isWX: window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger',
    isIphone: /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent),
    getUrlParam: function (name,urlStr =  window.location.search.substr(1)) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r =urlStr.match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    },
    decodeBase64(str=""){
        return window.decodeURIComponent(window.atob(str));
     },
     delQueStr(url, ref) {
         var str = "";
         if (url.indexOf('?') != -1) {
             str = url.substr(url.indexOf('?') + 1);
         }
         else {
             return url;
         }
         var arr = "";
         var returnurl = "";
         var setparam = "";
         if (str.indexOf('&') != -1) {
             arr = str.split('&');
             for (let i in arr) {
                 if (arr[i].split('=')[0] != ref) {
                     returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
                 }
             }
             return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
         }
         else {
             arr = str.split('=');
             if (arr[0] == ref) {
                 return url.substr(0, url.indexOf('?'));
             }
             else {
                 return url;
             }
         }
     },
    isphone: function (v) {
        var a = /^1[3|4|5|6|7|8|9]\d{9}$/;
        var c = v.match(a);
        if (toString.call(c) == '[object Array]') {
            return c[0];
        } else {
            return false;
        }
    },
    random: function (start, end) {
        var params = arguments,
            random = Math.random();
        if (params.length < 2) {
            return random;
        }
        return start + Math.round((end - start) * random);
    },
    initShareFunc: function (data) {
        let uname = name || '';
        let that = this;
        wx.ready(function () {
            createjs.WebAudioPlugin.playEmptySound();
            var shareTitle = '测测你的隐藏大女主人设'; //【微信分享好友的标题
            var shareWord = '马上测测'; //【微信分享好友的描述
            var link = that.delQueStr(window.location.href,'openId'); //【微信分享出去的链接地址
            var imgUrl = 'https://h5.wufae.com/Pingan/womentest/static/imgs/share.jpg'; //【微信分享图标路径
            //监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
            wx.onMenuShareTimeline({
                title: shareTitle,
                desc: shareTitle,
                link: link,
                imgUrl: imgUrl,
                success: function (res) {
                    console.log('微信分享朋友圈成功')
                    //$.get('./web/index.php/api/h5/shview');
                }
            });
            //监听“分享给朋友”按钮点击、自定义分享内容及分享结果接口
            wx.onMenuShareAppMessage({
                title: shareTitle,
                desc: shareWord,
                link: link,
                imgUrl: imgUrl,
                success: function (res) {
                    console.log('分享给朋友成功')
                    //$.get('./web/index.php/api/h5/shview');
                }
            });
        });
        wx.error(function (res) {
            // alert(JSON.stringify(res))
        });
    },
    /*
      utils.drawImg({
          canvas:that.$refs.spirit1, //绘制区域
          name:'r1',  //帧图名称
          start:1,   //开始索引值
          end:3,    //结束索引值
          time:100  //帧频率
      });
  */
    dTimer: [],
    drawImg(params) {
        let that = this;
        let canvas = params.canvas;
        let iname = params.name;
        let start = params.start || 1;
        let end = params.end;
        let time = params.time || 100;
        /****************************/
        let width = $(canvas).width();
        let height = $(canvas).height();
        $(canvas).attr({ width: width, height: height });
        var ctx = canvas.getContext('2d');
        let imgs = [];
        for (let i = start; i <= end; i++) {
            let img = new Image();
            img.src = 'static/imgs/spirit/' + iname + '_' + i + '.png';
            imgs.push(img);
        }
        let curIndex = 0;
        this.dTimer.push(
            setInterval(() => {
                ctx.clearRect(0, 0, width, height);
                let img = imgs[curIndex];
                ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);
                curIndex += 1;
                if (curIndex >= end) {
                    curIndex = 0;
                }
            }, time)
        );
    },
    stopDraw() {
        this.dTimer.forEach(v => {
            clearInterval(v);
        });
        this.dTimer = [];
    },
    /*       
      that.video1 = utils.playVideo({
              wraper:that.$refs.vView1, //视频外层
              vname:'ques',   //视频名称
              vend:'v1ToEnd', //结束封面Boolen
              poster:'static/img/vb.jpg?v=0.2',  //封面
              onplay:()=>{
                  
              },
              onpause:()=>{  
                  
              },
              callback:()=>{ //结束播放

              }
          });
  */
    playVideo: function (vObj) {
        var that = this;
        var div = vObj.wraper;
        var totalTime = vObj.total || 0;
        var name = vObj.vname;
        var callback = vObj.callback;
        var onplay = vObj.onplay;
        var oncanplay = vObj.oncanplay;
        var onpause = vObj.onpause;
        var poster = vObj.poster || '';
        var vend = vObj.vend || '';
        /*******************VIDEO**************************/
        var video = document.createElement('video');
        video.className = 'video';
        video.poster = poster;
        video.src = name.indexOf('http') != -1 ? name : 'static/video/' + name + '.mp4';
        video.setAttribute('preload', 'auto');
        video.style.width = window.innerWidth + 'px';
        video.style.height = window.innerHeight + 'px';
        video.style['object-fit'] = 'cover';
        video.onended = function () {
            video.className = 'video';
            callback && callback();
        };
        video.onplay = function () {
            let canplay = video.getAttribute('canplay');
            if (canplay != 'true') {
                that.vtimer = setInterval(() => {
                    let cplay = video.getAttribute('canplay');
                    if (cplay == 'true') {
                        onplay && onplay();
                        painter.bgMusic && painter.bgMusic.stop();
                        painter.play = false;
                        painter.playShow = false;
                        setTimeout(() => {
                            painter[vend] = true;
                        }, 100);
                        clearInterval(that.vtimer);
                        return false;
                    }
                }, 100);
                return false;
            } else {
                clearInterval(that.vtimer);
            }
            onplay && onplay();
            painter.bgMusic && painter.bgMusic.stop();
            painter.play = false;
            painter.playShow = false;
            setTimeout(() => {
                painter[vend] = true;
            }, 100);
        };
        video.oncanplay = function () {
            video.setAttribute('canplay', true);
            oncanplay && oncanplay();
            painter.hideTips();
        };
        video.onpause = function () {
            if (!video.ended) {
                onpause && onpause();
                painter[vend] = false;
            }
        };
        video.onclick = function () {
            if (video.paused) video.play();
        };
        if (that.isIphone) {
            video.setAttribute('webkit-playsinline', '');
            video.setAttribute('playsinline', '');
        } else {
            video.setAttribute('webkit-playsinline', 'true');
            video.setAttribute('playsinline', 'true');
            video.setAttribute('x-webkit-airplay', 'allow');
            video.setAttribute('x5-video-player-type', 'h5');
            video.setAttribute('x5-video-player-fullscreen', 'true');
            video.setAttribute('x5-video-orientation', 'portrait');
            video.setAttribute('x5-video-player-type', 'h5');
        }
        div.appendChild(video);
        painter.canPlay = true;
        return video;
        /*******************VIDEO**************************/
        /*if (that.isIphone) {
        var video = document.createElement("video");
        video.className = 'video';
        if (poster) video.poster = poster;
        video.src = name.indexOf('http') != -1 ? name : 'static/video/' + name + '.mp4';
        video.setAttribute("preload", "load");
        video.setAttribute("webkit-playsinline", "true");
        video.setAttribute("playsinline", "true");
        video.setAttribute("x5-playsinline", "true");
        //video.style.width = 410*window.innerWidth640/ + "px";
        //video.style.height = 230*640/window.innerWidth + "px";
        video.style['object-fit'] = 'fill';
        video.style['pointer-events'] = 'none';
        video.onended = function() {
          video.className = 'video';
          callback && callback();
        };
        video.onplay = function() {
          let canplay = video.getAttribute('canplay');
          if (canplay != 'true') {
            that.vtimer = setInterval(() => {
              let cplay = video.getAttribute('canplay');
              if (cplay == 'true') {
                onplay && onplay();
                painter.bgMusic && painter.bgMusic.stop();
                painter.play = false;
                painter.playShow = false;
                setTimeout(() => {
                  painter[vend] = true;
                }, 100);
                clearInterval(that.vtimer);
                return false;
              }
            }, 100);
            return false;
          } else {
            clearInterval(that.vtimer);
          }
          onplay && onplay();
          painter.bgMusic && painter.bgMusic.stop();
          painter.play = false;
          painter.playShow = false;
          setTimeout(() => {
            painter[vend] = true;
          }, 100);
        };
        video.oncanplay = function() {
          video.setAttribute('canplay', true);
          painter.hideTips();
        };
        video.onpause = function() {
          if (!video.ended) {
            onpause && onpause();
            painter[vend] = false;
          }
        };
        video.onclick = function() {
          if (video.paused) video.play();
        }
        div.appendChild(video);
        return video;
      } else {
        var canvas = document.createElement('canvas');
        canvas.style.pointerEvents = "none";
        canvas.className = 'video';
        div.appendChild(canvas);
        var player = new JSMpeg.Player('static/video/video.ts', {
          canvas: canvas,
          autoplay: false,
          loop: false,
          onEnded: function() {
            callback && callback();
          }
        });
        //player.play();
        return player;
      }
      */
    }
};
//utils.resizeRem();
export default utils
//module.export = utils;
